//
//  OAuthViewController.swift
//  weibo1
//
//  Created by Apple on 15/11/27.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
class OAuthViewController: UIViewController {
    let webView = UIWebView()
    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
@objc  private  func returns() {
    
    dismissViewControllerAnimated(true, completion: nil)
    
    }
    @objc private func defaultAccount() {
        
        //本地代码 和js通讯
        let jsString = "document.getElementById('userId').value = '285822161@qq.com', document.getElementById('passwd').value = '285822161hui' "
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .Plain, target: self, action: "returns")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "defaultAccount")
        setUpOAuth()
    }

    //MARK: 加载授权网页
    private func setUpOAuth(){
        let urlstring = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        if let url = NSURL(string: urlstring){
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        }
    
    }

}

extension OAuthViewController :UIWebViewDelegate
{
    //MARK: web的代理方法  
    //关闭
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    //显示
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //https://api.weibo.com/oauth2/authorize
        // http://www.baidu.com/?code=2c5214518e140cbf945d296dd2bfe019
        //065d163ae679bdae9b465dd5e79ff50b
        let urlstring = request.URL?.absoluteString ?? ""
        //屏蔽没用的页面
        if urlstring.hasPrefix("https://api.weibo.com/"){
            return true
        }
        if !urlstring.hasPrefix("http://www.baidu.com"){
            return false
        }
        //获得code
        guard let query = request.URL?.query else{
            return false
        }
        let codestr = "code="
        let code = query.substringFromIndex(codestr.endIndex)
//        let co = query.substringToIndex(codestr.endIndex)
//        print(code)
//        print(query)
        UserAccountViewModel().setGainToken(code) { (error) -> () in
            if error != nil{
            SVProgressHUD.showInfoWithStatus("请求失败")
                return
            }
            
        }
        
        self.dismissViewControllerAnimated(false) { () -> Void in
            
            NSNotificationCenter.defaultCenter().postNotificationName(rootViewController, object: nil)
        }
        
        
        SVProgressHUD.showInfoWithStatus("登录成功")
        return false
    }
}


